using UnityEngine;
using UnityEngine.SceneManagement;

public class MapWindow : BaseWindow
{
	private static MapWindow _instance;

	public UIMiniMap panelMiniMap;

	public UIWorldMap panelWorldMap;

	public NJGMap mapScript;

	public UISlider sliderZoomWorldMap;

	public float stepZoom = 0.5f;

	public bool worldMapShow;

	public static MapWindow instance
	{
		get
		{
			if (_instance == null)
			{
				BCWDebug.LogError("instance is null" + StackTraceUtility.ExtractStackTrace(), "minimap");
			}
			else
			{
				BCWDebug.LogError("instance is not null", "minimap");
			}
			return _instance;
		}
	}

	private void Awake()
	{
		_instance = this;
		panelWorldMap.InitInst();
		if (panelMiniMap != null)
		{
			panelMiniMap.gameObject.SetActive(true);
		}
		if (mapScript != null)
		{
			mapScript.userMapTexture = Resources.Load<Texture2D>("MiniMaps/" + SceneManager.GetActiveScene().name);
		}
		else
		{
			BCWDebug.LogError("mapScript = null", "minimap");
		}
		BCWDebug.Log("userMapTexture != null" + mapScript.userMapTexture != null, "minimap");
	}

	private void OnDestroy()
	{
		_instance = null;
	}

	private void OnEnable()
	{
	}

	private void OnDisable()
	{
	}

	public override void OnPreShow(object userData)
	{
		base.gameObject.SetActive(true);
	}

	public override void OnHide()
	{
		base.gameObject.SetActive(false);
	}

	public void showWorldMap()
	{
		GameController.instance.hideAllPanels();
		WindowManager.instance.ShowWindow(typeof(MapWindow), false);
		if (GameInfoWindow.instance != null && GameController.instance != null && GameController.instance.curTypeGame == TypeGame.TDM)
		{
			GameInfoWindow.instance.tdmInfoUI.gameObject.SetActive(false);
		}
		if (GameController.instance.curTypeGame != TypeGame.deathmath)
		{
			returnPositionWorldMap();
			sliderZoomWorldMap.value = 0.2f;
			setZoomWorldMap(sliderZoomWorldMap.value);
			setSizeIconsOnMap(60);
			panelMiniMap.ToggleWorldMap();
			panelMiniMap.gameObject.SetActive(false);
			worldMapShow = true;
			if (panelWorldMap != null)
			{
				panelWorldMap.enabled = true;
			}
		}
	}

	public static void HideWorldMap()
	{
		if (instance != null)
		{
			instance.hideWorldMap();
		}
	}

	public void hideWorldMap()
	{
		if (panelWorldMap != null)
		{
			panelWorldMap.Hide();
			panelWorldMap.enabled = false;
		}
		if (GameInfoWindow.instance != null && GameController.instance != null && GameController.instance.curTypeGame == TypeGame.TDM)
		{
			GameInfoWindow.instance.tdmInfoUI.gameObject.SetActive(true);
		}
		setSizeIconsOnMap(30);
		worldMapShow = false;
	}

	public void setZoomWorldMap(float val)
	{
		val = panelWorldMap.minZoom + val * (panelWorldMap.maxZoom - panelWorldMap.minZoom);
		if (val < panelWorldMap.minZoom)
		{
			val = panelWorldMap.minZoom;
		}
		if (val > panelWorldMap.maxZoom)
		{
			val = panelWorldMap.maxZoom;
		}
		panelWorldMap.zoom = val;
	}

	public float GetZoomWorldMap()
	{
		return (panelWorldMap.zoom - panelWorldMap.minZoom) / (panelWorldMap.maxZoom - panelWorldMap.minZoom);
	}

	public void zoomWorldMapIn()
	{
		sliderZoomWorldMap.value = (panelWorldMap.zoom + stepZoom - panelWorldMap.minZoom) / (panelWorldMap.maxZoom - panelWorldMap.minZoom);
		setZoomWorldMap(sliderZoomWorldMap.value);
	}

	public void zoomWorldMapOut()
	{
		sliderZoomWorldMap.value = (panelWorldMap.zoom - stepZoom - panelWorldMap.minZoom) / (panelWorldMap.maxZoom - panelWorldMap.minZoom);
		setZoomWorldMap(sliderZoomWorldMap.value);
	}

	public void returnPositionWorldMap()
	{
		panelWorldMap.ResetPanning();
	}

	public void setSizeIconsOnMap(int curSize)
	{
		if (mapScript != null)
		{
			mapScript.iconSize = curSize;
		}
	}

	public static void ResetBoundsMap()
	{
		if (instance != null)
		{
			instance.resetBoundsMap();
		}
	}

	public void resetBoundsMap()
	{
		Invoke("resetBounds", 0.2f);
	}

	private void resetBounds()
	{
		if (mapScript != null)
		{
			mapScript.UpdateBounds();
		}
	}
}
